<?php

namespace App\Console\Commands;

use App\Common\Constant\Params;
use App\Models\Merchant;
use App\Models\OldOrder;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\OrderGoods;
use App\Models\OrderOffer;
use App\Models\OrderStatus;
use App\Models\Province;
use App\Services\IStorageService;
use DB;
use Illuminate\Console\Command;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Collection;
use Log;

class generateProvince extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate:province';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成省市区静态文件';

    private $exceptionHandler;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(ExceptionHandler $exceptionHandler)
    {
        parent::__construct();

        $this->exceptionHandler = $exceptionHandler;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $provinces = Province::with('children')->where('parentid',0)->get();
        $provinces->transform(function ($province, $key){
            $this->provinceIds[] = $province->id;
            $province->children->transform(function ($city, $key){
                $this->cityIds[] = $city->id;
                return [
                    'i'=>$city->id,
                    'n'=>$city->name,
//                    'g'=>'c',//city
                ];
            });
            return [
                'i'=>$province->id,
                'n'=>$province->name,
//                'g'=>'p',//province
                'c'=>$province->children,
            ];
        });

//        dump($this->cityIds);
        $cities = Province::with('children')->whereIn('id',$this->cityIds)->get();
        $cities->map(function ($city, $key) use($provinces) {
            $city->children->transform(function ($district, $key){
                $this->districtIds[] = $district->id;
                return [
                    'i'=>$district->id,
                    'n'=>$district->name,
//                    'g'=>'d',//district
                ];
            });
            $provinces->push([
                'i'=>$city->id,
                'n'=>$city->name,
                'p'=>$city->parentid,
//                'g'=>'c',//city
                'c'=>$city->children,
            ]);
        });

        $districts = Province::whereIn('id',$this->districtIds)->get();
        $districts->map(function ($district, $key) use($provinces) {
            $provinces->push([
                'i'=>$district->id,
                'n'=>$district->name,
                'p'=>$district->parentid,
            ]);
        });

        $areas = $provinces->keyBy('i')->toJson(JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $provinceIds = Collection::make($this->provinceIds)->toJson();
        $provinces_count = $provinces->count();

        $js = sprintf("var areas = %s;var provinceIds = %s;module.exports = {areas:areas,provinceIds:provinceIds};",$areas,$provinceIds);
        $areasjs = file_put_contents(resource_path('static/areas.js'),$js,LOCK_EX) ? $provinces_count : 'fail';

        $php = sprintf('<?php $areas = \'%s\';$provinceIds = \'%s\';return ["areas"=>$areas,"provinceIds"=>$provinceIds];',$areas,$provinceIds);
        $areasphp = file_put_contents(resource_path('static/areas.php'),$php,LOCK_EX) ? $provinces_count : 'fail';

        $this->info("areasjs: $areasjs");
        $this->info("areasphp: $areasphp");

    }
}
